#!/usr/bin/python
# -*- coding:utf-8 -*-
import StringIO
import gzip
import ssl
import urllib2
import ConfigParser


class Downloader(object):
    def __init__(self):
        cf = ConfigParser.ConfigParser()
        cf.read('config.ini')
        cookie = cf.get('opener', 'cookie')
        user_agent = cf.get('opener', 'user-agent')
        self.headers = {
            'Cookie': cookie,
            'Host': 'www.zhihu.com',
            'Referer': 'http://www.zhihu.com/',
            'User-Agent': user_agent,
            'Accept-Encoding': 'gzip'
        }
        opener = urllib2.build_opener(urllib2.ProxyHandler())
        urllib2.install_opener(opener)

    def do_download(self, url):
        """
        根据网页地址下载网页
        :param self:
        :param url: 网页地址
        :return: 爬取成功以字符串方式返回网页代码，爬取失败返回None
        """
        if url is None:
            return None
        try:
            request = urllib2.Request(url=url, headers=self.headers)
            response = urllib2.urlopen(request, timeout=15)

            if response.getcode() == 200 and response.info().get('Content-Encoding') == 'gzip':
                content = response.read()
                data = StringIO.StringIO(content)
                gz = gzip.GzipFile(fileobj=data)
                content = gz.read()
                gz.close()
                return content
            return None
        except urllib2.HTTPError, e1:
            print u"open failed，address:%s,code:%d" % (url, e1.code)
            return None
        except urllib2.URLError, e2:
            print u"open failed，info：%s" % e2
            return None
        except ssl.SSLError:
            print u"timed out."
            return None
